home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / delivery / deliver.tz / deliver / uid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-24  |  2.7 KB  |  146 lines

  1. /* $Header: uid.c,v 2.1 89/06/09 12:25:41 network Exp $
  2.  *
  3.  * I wish the System V "id" program were universally available; but it
  4.  * isn't, so I've written this replacement.
  5.  *
  6.  * usage: uid [-options]
  7.  *
  8.  * Default action is to print one line in the manner of "id":
  9.  *      uid=201(chip) gid=50(group) euid=0(root) egid=0(root)
  10.  * (Note that the "euid" and "egid" entries are not output if they are
  11.  * the same as the "uid" and "gid" values.)
  12.  *
  13.  * If an option string is specified, it disables the normal behavior in
  14.  * favor of displaying id information, one per line, in the order that
  15.  * the options appear in the option string.  Legal options are:
  16.  *      u       real uid
  17.  *      g       real gid
  18.  *      U       effective uid
  19.  *      G       effective gid
  20.  *
  21.  * NOTE: This program is not a paragon of good style.
  22.  *       It's just something I needed for a Makefile.
  23.  *
  24.  * $Log:    uid.c,v $
  25.  * Revision 2.1  89/06/09  12:25:41  network
  26.  * Update RCS revisions.
  27.  * 
  28.  * Revision 1.3  89/06/09  12:24:00  network
  29.  * Baseline for 2.0 release.
  30.  * 
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <pwd.h>
  35. #include <grp.h>
  36. #include "config.h"
  37.  
  38. #ifdef NULL
  39. #undef NULL
  40. #endif
  41. #define NULL 0
  42.  
  43. extern  struct passwd   *getpwuid();
  44. extern  struct group    *getgrgid();
  45.  
  46. char    *progname = "uid";
  47.  
  48. char    *uid_desc();
  49. char    *gid_desc();
  50.  
  51. main(argc, argv)
  52. int     argc;
  53. char    **argv;
  54. {
  55.     int     uid, gid, euid, egid;
  56.     int     c, lines, errcount;
  57.  
  58.     uid = getuid();
  59.     gid = getgid();
  60.     euid = geteuid();
  61.     egid = getegid();
  62.  
  63.     errcount = 0;
  64.     lines = 0;
  65.  
  66.     while ((c = getopt(argc, argv, "ugUG")) != EOF)
  67.     {
  68.         switch (c)
  69.         {
  70.         case 'u':
  71.             (void) printf("%s\n", uid_desc(uid));
  72.             ++lines;
  73.             break;
  74.  
  75.         case 'g':
  76.             (void) printf("%s\n", gid_desc(gid));
  77.             ++lines;
  78.             break;
  79.  
  80.         case 'U':
  81.             (void) printf("%s\n", uid_desc(euid));
  82.             ++lines;
  83.             break;
  84.  
  85.         case 'G':
  86.             (void) printf("%s\n", gid_desc(egid));
  87.             ++lines;
  88.             break;
  89.  
  90.         case '?':
  91.             ++errcount;
  92.             break;
  93.         }
  94.     }
  95.  
  96.     if (errcount)
  97.     {
  98.         (void) fprintf(stderr, "usage: uid [-ugUG]\n");
  99.         exit(1);
  100.     }
  101.  
  102.     if (lines == 0)
  103.     {
  104.         (void) printf("uid=%s", uid_desc(uid));
  105.         (void) printf(" gid=%s", gid_desc(gid));
  106.  
  107.         if (euid != uid)
  108.             (void) printf(" euid=%s", uid_desc(euid));
  109.         if (egid != gid)
  110.             (void) printf(" egid=%s", gid_desc(egid));
  111.  
  112.         (void) printf("\n");
  113.     }
  114.  
  115.     exit(0);
  116.     /* NOTREACHED */
  117. }
  118.  
  119. char *
  120. uid_desc(uid)
  121. int     uid;
  122. {
  123.     struct passwd *pw;
  124.     static char buf[80];
  125.  
  126.     (void) sprintf(buf, "%d", uid);
  127.     if ((pw = getpwuid(uid)) != NULL)
  128.         (void) sprintf(buf + strlen(buf), "(%s)", pw->pw_name);
  129.  
  130.     return buf;
  131. }
  132.  
  133. char *
  134. gid_desc(gid)
  135. int     gid;
  136. {
  137.     struct group *gr;
  138.     static char buf[80];
  139.  
  140.     (void) sprintf(buf, "%d", gid);
  141.     if ((gr = getgrgid(gid)) != NULL)
  142.         (void) sprintf(buf + strlen(buf), "(%s)", gr->gr_name);
  143.  
  144.     return buf;
  145. }
  146.